package com.ruoyi.mall.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.mapper.PmsCategoryMapper;
import com.ruoyi.mall.domain.PmsCategory;
import com.ruoyi.mall.service.IPmsCategoryService;
import org.springframework.util.StringUtils;

/**
 * 商品类别Service业务层处理
 *
 * @author zyy
 * @date 2023-09-13
 */
@Service
public class PmsCategoryServiceImpl implements IPmsCategoryService {
    @Autowired(required = false)
    private PmsCategoryMapper pmsCategoryMapper;

    /**
     * 查询商品类别
     *
     * @param id 商品类别主键
     * @return 商品类别
     */
    @Override
    public PmsCategory selectPmsCategoryById(Long id) {
        return pmsCategoryMapper.selectPmsCategoryById(id);
    }

    /**
     * 查询商品类别列表
     *
     * @param pmsCategory 商品类别
     * @return 商品类别
     */
    @Override
    public List<PmsCategory> selectPmsCategoryList(PmsCategory pmsCategory) {
        return pmsCategoryMapper.selectPmsCategoryList(pmsCategory);
    }

    /**
     * 新增商品类别
     *
     * @param pmsCategory 商品类别
     * @return 结果
     */
    @Override
    public int insertPmsCategory(PmsCategory pmsCategory) {   //1.获取新增的类别的名称
        String categoryName = pmsCategory.getName();
        //1.1判断类名名称是否为空
        if (StringUtils.isEmpty(categoryName)) {
            throw new RuntimeException("新增的类别名称不能为空！");
        }
        //1.2判断新增的类别名称是否存在
        PmsCategory pmsCategory1 = pmsCategoryMapper.selectCategoryByName(categoryName);
        if (pmsCategory1 != null) {
            throw new RuntimeException("类别已存在，新增失败");
        }
        int dept = 1;
        //2.基于新增的节点的父级id查询父级节点的信息
        Long parentId = pmsCategory.getParentId();//=0
        PmsCategory parentCategory = null;
        if (parentId != 0) {//说明当前节点是一个子级节点
            //基于获取的父级节点的id获取父级节点的信息
            parentCategory = pmsCategoryMapper.selectParentCategoryByParentId(parentId);
            if (parentCategory.getName() == null) {
                throw new RuntimeException("添加类别失败，对应的父级类别名称不存在");
            } else {
                //说明新增节点的父级节点存在，让当前新增节点的深度在它所属的父级节点的深度的值基础上+1
                dept = parentCategory.getDepth() + 1;
            }
        }
        Date date = new Date();
        pmsCategory.setIsParent(0);
        pmsCategory.setDepth(dept);
        pmsCategory.setGmtCreate(date);
        pmsCategory.setGmtModified(date);
        int row = pmsCategoryMapper.insertPmsCategory(pmsCategory);
        if (row == 0) {
            throw new RuntimeException("商品类别新增失败");
        }
        /*如果当前的新增节点它不是父级节点，并且当前节点对应的父级节点isParent=0，
            则需要修改当前的节点父级节点的isParent=1
         */
        if (parentId != 0 && parentCategory.getIsParent() == 0) {
            PmsCategory updateParentCategory = new PmsCategory();
            updateParentCategory.setId(parentId);//基于父级id修改父级节点的isParent的值
            updateParentCategory.setIsParent(1);
            int i = pmsCategoryMapper.updatePmsCategory(updateParentCategory);
            if (i != 1) {
                System.out.println("父级节点的isParent值修改失败");
                throw new RuntimeException("新增失败");
            }
        }
        return row;
    }

    /**
     * 修改商品类别
     *
     * @param pmsCategory 商品类别
     * @return 结果
     */
    @Override
    public int updatePmsCategory(PmsCategory pmsCategory) {   //1.获取修改的商品类别的名称
        String updateCategory = pmsCategory.getName();
        //1.1判断类别名称是否为空
        if (StringUtils.isEmpty(updateCategory)) {
            throw new RuntimeException("修改的类别名称不能为null");
        }
        //1.2判断修改的商品类别名称是否重复
        PmsCategory category = pmsCategoryMapper.selectCategoryByName(updateCategory);
        if (category != null) {
            if (category.getName().equals(updateCategory) && category.getId() != pmsCategory.getId()) {
                throw new RuntimeException("该商品类别名称已存在，请重新命名商品类别名称");
            }
        }
        pmsCategory.setGmtModified(new Date());
        return pmsCategoryMapper.updatePmsCategory(pmsCategory);
    }

    /**
     * 批量删除商品类别
     *
     * @param ids 需要删除的商品类别主键
     * @return 结果
     */
    @Override
    public int deletePmsCategoryByIds(Long[] ids) {
        return pmsCategoryMapper.deletePmsCategoryByIds(ids);
    }

    /**
     * 删除商品类别信息
     *
     * @param id 商品类别主键
     * @return 结果
     */
    @Override
    public int deletePmsCategoryById(Long id) {   //1.根据id去查询数据
        PmsCategory category = pmsCategoryMapper.selectCategoryByIds(id);
        if (category == null) {
            throw new RuntimeException("该类别数据不存在");
        }
        //2.判断要删除的节点下是否存在子级节点
        if (category.getIsParent() == 1) {//该节点为父级节点
            //基于父级节点的id去查询对应的子级节点的个数
            int count = pmsCategoryMapper.countByParentId(id);
            if (count > 0) {
                throw new RuntimeException("该类别下存在子级类别，请先删除子级类别");
            }
        }
        //3.执行删除操作
        int row = pmsCategoryMapper.deletePmsCategoryById(id);
        if (row <= 0) {
            throw new RuntimeException("删除失败");
        }
        //4.如果父级类别已经没有任何的子级类别，则父级类别的is_parent值改为0
        int num = pmsCategoryMapper.countByParentId(category.getParentId());
        if (num <= 0) {
            //基于父级节点id将父级节点的isParent值改为0
            PmsCategory category1 = new PmsCategory();
            category1.setId(category.getParentId());
            category1.setIsParent(0);
            category1.setGmtModified(new Date());
            int rows = pmsCategoryMapper.updatePmsCategory(category1);
            if (rows <= 0) {
                throw new RuntimeException("修改失败");
            }
        }
        return row;
    }
}
